<template>
  <!-- 搜索工作栏 -->
  <ContentWrap ref="form">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="业务流水号" prop="seq">
        <el-input
          v-model.tirm="queryParams.seq"
          placeholder="请输入业务流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-input
          v-model.tirm="queryParams.businessType"
          placeholder="请输入业务类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="operateType">
        <el-input
          v-model.tirm="queryParams.operateType"
          placeholder="请输入操作类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否成功" prop="success">
        <el-select v-model="queryParams.success" placeholder="请选择是否成功" clearable>
          <el-option
            v-for="dict in getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模块" prop="moduleId">
        <el-select v-model.trim="queryParams.moduleId" clearable placeholder="请选择模块">
          <el-option
            v-for="item in moduleList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="请求发送时间" prop="reqTime">
        <el-date-picker
          v-model="queryParams.reqTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="请求返回时间" prop="respTime">
        <el-date-picker
          v-model="queryParams.respTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:data-push-log:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap :setPadding="false">
    <el-table v-loading="loading" :data="list" stripe :max-height="tableHeight">
      <el-table-column label="id" prop="id" show-overflow-tooltip />
      <el-table-column label="业务流水号" prop="seq" show-overflow-tooltip />
      <el-table-column label="模块名" prop="moduleName" show-overflow-tooltip />
      <el-table-column label="业务类型" prop="businessType" show-overflow-tooltip />
      <el-table-column label="操作类型" prop="operateType" show-overflow-tooltip />
      <el-table-column label="是否成功" prop="success">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.success" />
        </template>
      </el-table-column>
      <el-table-column
        label="请求发送时间"
        prop="reqTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="请求返回时间"
        prop="respTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row.id)"
            v-hasPermi="['system:data-push-log:query']"
          >
            详细
          </el-button>
          <el-button
            link
            type="warn"
            @click="handleRetry(scope.row.id)"
            v-hasPermi="['system:data-push-log:retry']"
          >
            重试
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <DataPushLogDetail ref="detailRef" />
</template>

<script setup lang="ts" name="DataPushLog">
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import DataPushLogDetail from './DataPushLogDetail.vue'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as DataPushLogApi from '@/api/system/dataPushLog'
import * as ModuleApi from '@/api/system/module'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  reqRaw: null,
  success: undefined,
  seq: null,
  respRaw: null,
  errorMsg: null,
  moduleId: null,
  businessType: null,
  operateType: null,
  reqTime: [],
  respTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const moduleList = ref([]) //学校列表

/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DataPushLogApi.getDataPushLogPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 重试按钮操作 */
const handleRetry = async (id: number) => {
  try {
    // 重试的二次确认
    await message.syncConfirm()
    // 发起删除
    await DataPushLogApi.retryDataPush(id)
    message.success(t('common.createSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DataPushLogApi.exportDataPushLog(queryParams)
    download.excel(data, '数据推送日志.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  moduleList.value = await ModuleApi.getModuleList([])
  getList()
})
</script>
